/*
 *  Licensed Materials - Property of IBM
 *  5725-G92 (C) Copyright IBM Corp. 2011, 2012. All Rights Reserved.
 *  US Government Users Restricted Rights - Use, duplication or
 *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

/**
 *  WL.Server.invokeHttp(parameters) accepts the following json object as an argument:
 *  
 *  {
 *  	// Mandatory 
 *  	method : 'get' , 'post', 'delete' , 'put' or 'head' 
 *  	path: value,
 *  	
 *  	// Optional 
 *  	returnedContentType: any known mime-type or one of "json", "css", "csv", "javascript", "plain", "xml", "html"  
 *  	returnedContentEncoding : 'encoding', 
 *  	parameters: {name1: value1, ... }, 
 *  	headers: {name1: value1, ... }, 
 *  	cookies: {name1: value1, ... }, 
 *  	body: { 
 *  		contentType: 'text/xml; charset=utf-8' or similar value, 
 *  		content: stringValue 
 *  	}, 
 *  	transformation: { 
 *  		type: 'default', or 'xslFile', 
 *  		xslFile: fileName 
 *  	} 
 *  } 
 */

/**
 * @param interest
 *            must be one of the following: world, africa, sport, technology,
 *            ... (The list can be found in
 *            http://edition.cnn.com/services/rss/)
 * @returns json list of items
 */
//function getTasks(interest) {
//	path = getPath(interest);
//	var input = {
//		method : 'get',
//		returnedContentType : 'xml',
//		path : 'slm/webservice/1.40/project.xml',
//	// parameters: {
//	// title:'My Tasks',
//	// query:'((Owner.Name = muthyamdgem@gmail.com) and (State != Completed))',
//	// order:'Rank'
//	// //,fetch:true
//	// }
//	};
//	return WL.Server.invokeHttp(input);
//}
/**
 * 
 * @param interest
 *            must be one of the following: world, africa, sport, technology,
 *            ... (The list can be found in
 *            http://edition.cnn.com/services/rss/)
 * @returns json list of items
 */
function getTasksFiltered(interest) {
	path = getPath(interest);

	var input = {
		method : 'get',
		returnedContentType : 'xml',
		path : path,
		transformation : {
			type : 'xslFile',
			xslFile : 'filtered.xsl'
		}
	};

	return WL.Server.invokeHttp(input);
}

function getPath(interest) {
	if (interest == undefined || interest == '') {
		interest = '';
	} else {
		interest = '_' + interest;
	}
	return 'rss/edition' + interest + '.rss';
}

function getWorkspace() {
	var input = {
		method : 'get',
		returnedContentType : 'xml',
		path : 'slm/webservice/1.40/workspace.xml',
		parameters : {
			'type' : 'workspace',
			'title' : 'workSpace',
			'start' : '1',
			'pagesize' : '20',
			'fetch' : 'Name',
			'order' : 'Name Asc'
		}
	};
	return WL.Server.invokeHttp(input);
}

function getProjects() {
	var input = {
		method : 'get',
		returnedContentType : 'xml',
		path : 'slm/webservice/1.40/project.xml',
		parameters : {
			'type' : 'project',
			'key' : 'Projects',
			'start' : '1',
			'pagesize' : '20',
			'fetch' : 'Name,ObjectID',
			'order' : 'Name Asc'
		}
	};
	return WL.Server.invokeHttp(input);
}

function getUserStories(projectID) {
	var input = {
		method : 'get',
		returnedContentType : 'xml',
		path : 'slm/webservice/1.40/hierarchicalrequirement.xml',
		parameters : {
			'type' : 'hierarchicalrequirement',
			'key' : 'userStories',
			'start' : '1',
			'pagesize' : '20',
			'fetch' : 'true',
			'query' : '((FormattedID = "US6") AND (ObjectID = "10139179123"))',
			'order' : 'Name Asc'
		}
	};
	return WL.Server.invokeHttp(input);
}

function getTasks(interest) {
	path = getPath(interest);
	var input = {
		method : 'get',
		returnedContentType : 'xml',
		path : 'slm/webservice/1.40/task.xml',
		parameters : {
			'type' : 'task',
			'key' : 'Tasks',
			'start' : '1',
			'pagesize' : '20',
			'fetch' : 'Project',
			//'query' : '((ObjectID = "10139255107") AND (WorkProduct.ObjectID = "10139071717"))',
			//'query' : '(ObjectID = "'+interest+'")',
			'order' : 'Name Asc'
		}
	};
	return WL.Server.invokeHttp(input);
}
